import {
  createApp
} from "vue";
import router from "./router/index";
import store from "../src/store/index";
import './styles/common.scss'
import ElementPlus from "element-plus";
import zhCn from "element-plus/es/locale/lang/zh-cn";
import "element-plus/dist/index.css";
import App from "./App.vue";
import * as ELIcons from "@element-plus/icons-vue";
import { AdminCompanyInfo } from "@/api/settings.js"
import config from "./config";

// 先拿到 系统的基础配置（title，logo）再启动挂载整个应用
const configInfo = localStorage.getItem("configInfo");
if (configInfo) {
  let info = JSON.parse(configInfo)
  config.title = info.companyName
  config.logo = info.companyLogoUrl
  // 等待系统配置加载完成后再启动应用
  const app = createApp(App);
app.config.globalProperties.$icons = []
for (const [key, component] of Object.entries(ELIcons)) {
    app.config.globalProperties.$icons.push(key)
    app.component(key, component)
}
  app.use(router).use(store).use(ElementPlus, {
    locale: zhCn,
  });
  app.mount("#app");
} else {
  AdminCompanyInfo().then((res) => {
    console.log('sasas', res);
    config.title = res.companyName
    config.logo = res.companyLogoUrl
    // 存一份在本地，刷新页面的时候就不需要调用接口了
    localStorage.setItem("configInfo", JSON.stringify(res));
  }).finally(() => {
    // 等待系统配置加载完成后再启动应用
    const app = createApp(App);
    // 小图标引用
    for (const iconName in ELIcons) {
      // 注册组件
      app.component(iconName, ELIcons[iconName]);
    }

    app.use(router).use(store).use(ElementPlus, {
      locale: zhCn,
    });
    // app.use(hljsVuePlugin) // 引入代码高亮，并进行全局注册
    app.mount("#app");
  })
}


